package com.wondersgroup.framework.sample.component.data;

import com.wondersgroup.framework.sample.combination.vo.UserQueryVO;
import com.wondersgroup.framework.sample.component.form.vo.UserVO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserData {
	private ServletContext context;

	public UserData(HttpServletRequest request) {
		this.context = request.getSession().getServletContext();
	}

	public UserVO loadUserById(String id) {
		List userData = (List) this.context.getAttribute("userList");
		for (int i = 0; i < userData.size(); ++i) {
			UserVO userVO = (UserVO) userData.get(i);
			if (userVO.getId().equals(id))
				return userVO;
		}

		return null;
	}

	public void saveUser(UserVO userVO) {
		List userData = (List) this.context.getAttribute("userList");
		userData.add(0, userVO);
		this.context.setAttribute("userList", userData);
	}

	public void updateUser(UserVO userVO) {
		removeUser(userVO.getId());
		saveUser(userVO);
	}

	public void removeUser(String id) {
		List userData = (List) this.context.getAttribute("userList");
		for (int i = 0; i < userData.size(); ++i) {
			UserVO temp = (UserVO) userData.get(i);
			if (temp.getId().equals(id)) {
				userData.remove(i);
				this.context.setAttribute("userList", userData);
				return;
			}
		}
	}

	public List<UserVO> queryUser(UserQueryVO queryVO) {
		List userData = (List) this.context.getAttribute("userList");
		String query = queryVO.getQuery();
		String id = queryVO.getId();
		String name = queryVO.getName();
		String census = queryVO.getCensus();
		String sex = queryVO.getSex();

		if ((isNull(id)) && (isNull(name)) && (isNull(census)) && (isNull(sex))) {
			return userData;
		}

		List result = new ArrayList();
		Iterator i$ = userData.iterator();
		label266: while (true) {
			UserVO user;
			while (true) {
				while (true) {
					while (true) {
						while (true) {
							if (!(i$.hasNext()))
								break label266;
							user = (UserVO) i$.next();
							if ((isNull(id)) || (user.getId() == null)
									|| (user.getId().equals(id)))
								break;
						}

						if ((isNull(name)) || (user.getName() == null)
								|| (user.getName().equals(name)))
							break;
					}

					if ((isNull(census)) || (user.getCensus() == null)
							|| (user.getCensus().equals(census)))
						break;
				}

				if ((isNull(sex)) || (user.getSex() == null)
						|| (user.getSex().equals(sex)))
					break;
			}

			result.add(user);
		}

		return result;
	}

	private boolean isNull(String parameter) {
		return ((parameter == null) || (parameter.equals("")));
	}
}